<template>
	<view class="add-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="类型" prop="type">
					<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择类型" @click="typeShow = true" />
					<u-select :list="typeList" v-model="typeShow" @confirm="typeConfirm"></u-select>
				</u-form-item>

				<u-form-item class="form-item" label="内容">
					<u-input v-model="form.content" type="textarea" placeholder="请输入内容" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">提交</u-button></view>
	</view>
</template>

<script>
import uniFileUpload from '@/components/uni-file-uploader.vue';
import { mapState } from 'vuex';
export default {
	components: {
		uniFileUpload
	},
	data() {
		return {
			id: '',
			navItem: 0,
			title: '提交待办事项',

			type: '',
			typeShow: false,
			typeList: [],

			form: {
				type: null,
				content: null
			},
			rules: {
				type: [
					{
						required: true,
						message: '请选择类型',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.loadDict('oa_todo_type', 'typeList');

		if (option && option.id) {
			this.id = option.id;
			this.title = this.title.replace('提交', '修改');
			this.loadData();
		}

		uni.setNavigationBarTitle({
			title: this.title
		});
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onUnload() {
		this.$store.commit('filePush', []);
	},
	computed: {
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		typeConfirm(e) {
			this.type = e[0].label;
			this.form.type = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.form.clientCode = this.userInfo.sysUser.clientCode;
				this.form.createBy = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		loadData() {
			this.$request('/oaAttndTodo/oa-attnd-todo/query/' + this.id).then((res) => {
				this.form = res.data.oaAttndTodo;
				let regx = /<[^>]*>|<\/[^>]*>/gm;
				if (this.form.content) {
					this.form.content = this.form.content.replace(regx, '');
				}

				this.typeList.forEach((item) => {
					if (this.form.type == item.value) {
						this.type = item.label;
					}
				});
			});
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				this[list] = [];
				res.data &&
					res.data.forEach((item) => {
						this[list].push({
							value: item.dictValue,
							label: item.dictName
						});
					});
			});
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				} else {
					let data = {};
					if (this.id) {
						this.form.id = this.id;
					}
					data = {
						oaAttndTodo: this.form
					};
					this.$request('/oaAttndTodo/oa-attnd-todo/add', data, {
						load: true
					}).then((res) => {
						this.$msg(res.msg);
						setTimeout(() => {
							uni.navigateBack();
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.add-view {
	background-color: #fff;
}

.form-box {
	border-top: 20rpx solid #f5f7fa;

	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
